﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Code.Models;
using Code.Commen;

namespace Code.BLL
{
    public class ProductManage:ManageBase<Product>
    {
        /// <summary>
        /// 新增产品
        /// </summary>
        public int Add(Product product,IEnumerable<ProductInfo> infos,IEnumerable<ProductPicture> pics)
        {
            if (product == null || infos == null || pics == null)
                throw new AppException("参数不足");
            int rs = 0;
            base.BeginTransaction();
            base.Insert<Product>(new { product.Identifier, product.TypeID });
            base.Execute(new SqlExpression("DECLARE @ID BIGINT SET @ID=@@IDENTITY"));
            base.Insert<ProductPicture>(pics.Select(p => new { p.FileName, p.IsDefault, ProductID = new SqlExpression("@ID"), p.TypeID }).ToArray());
            foreach (var item in infos)
            {
                if (item is ProductInfo_Ch)
                    base.Insert<ProductInfo_Ch>(new { ID=new SqlExpression("@ID"), item.Name,item.Discription});
                else if (item is ProductInfo_En)
                    base.Insert<ProductInfo_En>(new { ID = new SqlExpression("@ID"), item.Name, item.Discription });
            }
            rs = base.ExecuteTransaction();
            return rs;
        }

        /// <summary>
        /// 修改产品
        /// </summary>
        public int Edit(Product product, IEnumerable<ProductInfo> infos,IEnumerable<ProductPicture> ups, IEnumerable<ProductPicture> news,IEnumerable<long> delpic)
        {
            int rs = 0;
            base.BeginTransaction();
            
            if(product!=null)
                base.Update<Product>(new { product.Identifier, product.TypeID }, new { ID = product.ID });

            if (delpic != null)
                base.Delete<ProductPicture>(delpic.Select(p => new { ID = p }).ToArray());

            if (news != null && news.Count()>0)
                base.Insert<ProductPicture>(news.Select(p => new { p.FileName, p.IsDefault, ProductID = product.ID, p.TypeID }).ToArray());

            if (ups != null && ups.Count() > 0)
                base.Update<ProductPicture>(ups.ToNameValue(p => (object)new { p.IsDefault, p.TypeID }, p => (object)new { p.ID }));
            
            if (infos != null)
            {
                foreach (var item in infos)
                {
                    if (item is ProductInfo_Ch)
                        base.Update<ProductInfo_Ch>(new { item.Name, item.Discription }, new { ID = product.ID });
                    else if (item is ProductInfo_En)
                        base.Update<ProductInfo_En>(new { item.Name, item.Discription }, new { ID = product.ID });
                }
            }
            rs = base.ExecuteTransaction();
            return rs;
        }

        public int Del(params long[] id)
        {
            base.BeginTransaction();
            base.Delete<ProductInfo_Ch>(new { ID = new ComparValue(Comparison.In, id) });
            base.Delete<ProductInfo_En>(new { ID = new ComparValue(Comparison.In, id) });
            base.Delete<ProductPicture>(new { ProductID = new ComparValue(Comparison.In, id) });
            base.Delete<Product>(new { ID = new ComparValue(Comparison.In, id) });
            return base.ExecuteTransaction();
        }

        /// <summary>
        /// 移动产品到指定类型
        /// </summary>
        public int MoveByID(int type, params long[] ids)
        {
            int rs = 0;

            rs = base.Update<Product>(new { TypeID = type }, new { ID = new ComparValue(Comparison.In, ids) });

            return rs;
        }

        /// <summary>
        /// 新增分类
        /// </summary>
        public int AddType(ProductType type)
        {
            return base.Insert<ProductType>(new { type.ParentID, type.Name_En, type.Name_Ch, type.Parents });
        }

        /// <summary>
        /// 修改分类名称
        /// </summary>
        public int EditType(ProductType type)
        {
            return base.Update<ProductType>(new { type.Name_En, type.Name_Ch }, new { type.ID });
        }

        /// <summary>
        /// 删除分类，包括子级(不能有分类也属不能有产品)
        /// returns: -2 分类所属有产品
        ///          -1 系统错
        /// </summary>
        public int DelType(int id)
        {
            var s = base.Tables.Product.Any(p => p.ProductType.ID == id || p.ProductType.Parents.Contains(id + ","));
            if (s)
                return -2;
            base.BeginTransaction();
            base.Update<ProductType>(new { ParentID = null as int? }, new { Parents = new ComparValue(Comparison.Sql, new SqlExpression("LIKE {0}", "%," + id + ",%")) });
            base.Delete<ProductType>(new { Parents = new ComparValue(Comparison.Sql, new SqlExpression("LIKE {0}", "%," + id + ",%")) });
            base.Delete<ProductType>(new { ID = id });
            return base.ExecuteTransaction();

        }


        /// <summary>
        /// 编辑图片分类
        /// </summary>
        public int EditPicType(ProductPictureType type)
        {
            base.BeginTransaction();
            if (type.IsDefault)
            {
                base.Update<ProductPictureType>(new { IsDefault = false }, new { IsDefault = true, ID = new ComparValue(Comparison.UnEqual, type.ID) });
                base.Update<ProductPictureType>(new { type.Name_Ch, type.Name_En, type.IsDefault }, new { type.ID });
            }
            else
            {
                base.Update<ProductPictureType>(new { type.Name_Ch, type.Name_En }, new { type.ID });
            }
            return base.ExecuteTransaction();
        }

        /// <summary>
        /// 新增图片分类
        /// </summary>
        public int AddPicType(ProductPictureType type)
        {
            base.BeginTransaction();
            if (type.IsDefault)
            {
                base.Update<ProductPictureType>(new { IsDefault = false }, new { IsDefault = true });
            }
            base.Insert<ProductPictureType>(new { type.Name_Ch, type.Name_En, type.IsDefault });
            return base.ExecuteTransaction();
        }

        /// <summary>
        /// 删除图片分类(无法删除当前为默认的分类)
        /// </summary>
        public int DelPicType(int id)
        {
            var type = base.Tables.ProductPictureType.FirstOrDefault(p => p.IsDefault);
            if (type.ID==id)
                return -2;
            base.BeginTransaction();
            base.Update<ProductPicture>(new { TypeID = type.ID }, new { TypeID = id });
            base.Delete<ProductPictureType>(new { ID = id });
            return base.ExecuteTransaction();
        }
    }
}
